﻿
using System.Collections.Generic;
using Test30.Util;

namespace Test30.Engine
{
    public abstract class ContentReference<T> where T : class, INamed
    {
        public T Value
        {
            get
            {
                if (_cache != null) return _cache;
                T value = _collection.Find(_name);
                _cache = value;
                return value;
            }
        }
        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;

                    // clear the cache
                    _cache = null;
                }
            }
        }

        protected ContentReference(string name, IEnumerable<T> collection)
        {
            _name = name;
            _collection = collection;
        }
        protected abstract IEnumerable<T> GetCollection(Content content);


        private string _name;
        private readonly IEnumerable<T> _collection;
        private T _cache;
    }
}
